//
//  MinePageHeaderView.m
//  ShowYork
//
//  Created by hxh on 2021/3/5.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "MinePageHeaderView.h"

@interface MinePageHeaderView ()
@property (nonatomic,strong)UIImageView *headerIcon;
@property (nonatomic,strong)UIImageView *nextIcon;
@property (nonatomic,strong)UIImageView *descIcon;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UILabel *descLb;


@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIView *shadowView;

@property (nonatomic,strong)UILabel *likedLb;
@property (nonatomic,strong)UILabel *opusLb;
@property (nonatomic,strong)UILabel *dynamicLb;
@property (nonatomic,strong)UILabel *fansLb;

@property (nonatomic,strong)UIImageView *likedImgView;
@property (nonatomic,strong)UIImageView *opusImgView;
@property (nonatomic,strong)UIImageView *dynamicImgView;
@property (nonatomic,strong)UIImageView *fansImgView;

@property (nonatomic,strong)UIButton *likedButton;
@property (nonatomic,strong)UIButton *opusButton;
@property (nonatomic,strong)UIButton *dynamicButton;
@property (nonatomic,strong)UIButton *fansButton;
@property (nonatomic,strong)UIButton *coverBtn;

@property (nonatomic,strong)UIButton *nextBtn;
@property (nonatomic,strong)UIButton *descBtn;


@end

@implementation MinePageHeaderView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (instancetype)initWithUserInfoItem:(HomePageUserInfoModel *)userInfo{
    if (self = [super init]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(showUserInfo) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.coverBtn];
    
    
    self.headerIcon = [[UIImageView alloc]init];
    self.headerIcon.layer.cornerRadius = 36;
    self.headerIcon.layer.masksToBounds = YES;
    self.headerIcon.backgroundColor = RGBA(230, 230, 230, 1);
    [self addSubview:self.headerIcon];
    
    [self.headerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(16);
        make.width.height.mas_equalTo(72);
    }];
    
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.textColor = C51;
    self.nameLb.font = [UIFont boldSystemFontOfSize:18];
    [self addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerIcon.mas_right).offset(16);
        make.top.equalTo(self.headerIcon.mas_top).offset(12);
    }];
    self.descLb = [[UILabel alloc]init];
    self.descLb.textColor = C153;
    self.descLb.font = font(12);
    self.descLb.text = @"高考加油，高考必胜!";
    [self addSubview:self.descLb];
    [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom).offset(8);
        make.left.equalTo(self.nameLb);
        make.right.mas_lessThanOrEqualTo(self).offset(-60);
    }];
    
    
    
    self.shadowView             = [UIView new];
    [self addSubview:self.shadowView];
    
    self.shadowView.layer.shadowColor   = RGBA(240, 240, 240, 1).CGColor;
    self.shadowView.layer.shadowOffset  = CGSizeMake(0, 0);
    self.shadowView.layer.shadowOpacity = 0.8;
    self.shadowView.layer.shadowRadius  = 4;
    
    
    self.containView = [[UIView alloc]init];
    self.containView.layer.backgroundColor = [UIColor whiteColor].CGColor;
    self.containView.layer.shadowColor = RGBA(240, 240, 240, 1).CGColor;
    self.containView.layer.shadowOffset = CGSizeMake(0,0);
    self.containView.layer.shadowOpacity = 1;
    self.containView.layer.shadowRadius = 5;
    self.containView.layer.cornerRadius = 8;
    self.containView.layer.masksToBounds = YES;
    [self.shadowView addSubview:self.containView];
    
    
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.containView);
    }];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerIcon.mas_bottom).offset(24);
        make.left.equalTo(self).offset(16);
        make.right.equalTo(self).offset(-16);
        make.height.mas_equalTo(80);
        make.bottom.equalTo(self);
    }];
    
    
    
    self.likedLb = [[UILabel alloc]init];
    self.likedLb.text = @"我赞过的";
    self.likedLb.textColor = C51;
    self.likedLb.font = font(12);
    [self.shadowView addSubview:self.likedLb];

    self.dynamicLb = [[UILabel alloc]init];
    self.dynamicLb.text = @"我的动态";
    self.dynamicLb.textColor = C51;
    self.dynamicLb.font = font(12);
    [self.shadowView addSubview:self.dynamicLb];
    
    self.opusLb = [[UILabel alloc]init];
    self.opusLb.text = @"我的作品";
    self.opusLb.textColor = C51;
    self.opusLb.font = font(12);
    [self.shadowView addSubview:self.opusLb];

    
    
    self.fansLb = [[UILabel alloc]init];
    self.fansLb.text = @"关注/粉丝";
    self.fansLb.textColor = C51;
    self.fansLb.font = font(12);
    [self.shadowView addSubview:self.fansLb];

    
    self.likedImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_liked_tag"]];
    [self.shadowView addSubview:self.likedImgView];
    
    self.dynamicImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_dynamic_tag"]];
    [self.shadowView addSubview:self.dynamicImgView];
    
    
    self.opusImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_opus_tag"]];
    [self.shadowView addSubview:self.opusImgView];
    
    
    self.fansImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_fans_tag"]];
    [self.shadowView addSubview:self.fansImgView];
    
    self.likedButton = [[UIButton alloc]init];
    self.likedButton.tag = 0;
    [self.likedButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.shadowView addSubview:self.likedButton];
    
    self.dynamicButton = [[UIButton alloc]init];
    self.dynamicButton.tag = 1;
    [self.dynamicButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.shadowView addSubview:self.dynamicButton];
    
    self.opusButton = [[UIButton alloc]init];
    self.opusButton.tag = 2;
    [self.opusButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.shadowView addSubview:self.opusButton];
    

    self.fansButton = [[UIButton alloc]init];
    self.fansButton.tag = 3;
    [self.fansButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.shadowView addSubview:self.fansButton];
    
    [self.likedLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.likedButton);
        make.bottom.equalTo(self.shadowView).offset(-16);
    }];
    
    
    [self.dynamicLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.dynamicButton);
        make.bottom.equalTo(self.shadowView).offset(-16);
    }];
    
    [self.opusLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.opusButton);
        make.bottom.equalTo(self.shadowView).offset(-16);
    }];
    
    [self.fansLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.fansButton);
        make.bottom.equalTo(self.shadowView).offset(-16);
    }];
    
    
    [self.likedImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.likedLb.mas_top).offset(-8);
        make.centerX.equalTo(self.likedLb);
    }];
    
    [self.dynamicImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.dynamicLb.mas_top).offset(-8);
        make.centerX.equalTo(self.dynamicLb);
    }];
    
    [self.opusImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.opusLb.mas_top).offset(-8);
        make.centerX.equalTo(self.opusLb);
    }];
    
    [self.fansImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.fansLb.mas_top).offset(-8);
        make.centerX.equalTo(self.fansLb);
    }];
    
    [self.likedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.shadowView);
        make.right.equalTo(self.dynamicButton.mas_left);
        make.width.mas_equalTo(self.dynamicButton);
    }];
    
    [self.dynamicButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.shadowView);
        make.right.equalTo(self.opusButton.mas_left);
        make.width.mas_equalTo(self.opusButton);
    }];
    
    
    [self.opusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.shadowView);
        make.right.equalTo(self.fansButton.mas_left);
        make.width.mas_equalTo(self.fansButton);
    }];
    
    
    [self.fansButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self.shadowView);
        make.left.equalTo(self.opusButton.mas_right);
        make.width.mas_equalTo(self.likedButton);
    }];
    
    self.nextIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_access_tag"]];
    [self addSubview:self.nextIcon];
    [self.nextIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headerIcon);
        make.right.equalTo(self).offset(-32);
    }];
    
    self.descIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_desc_tag"]];
    [self addSubview:self.descIcon];
    [self.descIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.descLb.mas_right).offset(8);
        make.centerY.equalTo(self.descLb);
    }];
    
    
    self.nextBtn = [[UIButton alloc]init];
    [self.nextBtn addTarget:self action:@selector(showUserInfo) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self);
        make.bottom.equalTo(self.shadowView.mas_top);
        make.left.equalTo(self.nextIcon);
    }];
    
    self.descBtn = [[UIButton alloc]init];
    [self.descBtn addTarget:self action:@selector(descMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.descBtn];
    [self.descBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom).offset(4);
        make.left.equalTo(self.descLb);
        make.right.equalTo(self.descIcon);
        make.bottom.equalTo(self.descLb.mas_bottom).offset(4);
    }];
    
    
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.headerIcon);
        make.right.equalTo(self);
    }];
    
    self.headerIcon.userInteractionEnabled = YES;
    self.nameLb.userInteractionEnabled = YES;
    UITapGestureRecognizer *head_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    UITapGestureRecognizer *name_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    [self.headerIcon addGestureRecognizer:head_tap];
    [self.nameLb addGestureRecognizer:name_tap];

}

- (void)showUserInfo{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfo)]) {
        [self.delegate showUserInfo];
    }
}
- (void)descMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(updateUserDesc)]) {
        [self.delegate updateUserDesc];
    }
}

- (void)setUserInfo:(HomePageUserInfoModel *)userInfo{
    
    _userInfo = userInfo;
    
    self.nameLb.text = userInfo.realName;
    self.descLb.text = userInfo.userSign;
    
    if (userInfo.imagePath.length == 0) {
        if (userInfo.sex.intValue == 0) {
            self.headerIcon.image = [UIImage imageNamed:@"defaut_header"];
        }else if (userInfo.sex.intValue == 1){
            self.headerIcon.image = [UIImage imageNamed:@"common_boy_tag"];
        }else{
            self.headerIcon.image = [UIImage imageNamed:@"common_girl_tag"];
        }
    }else{
        [self.headerIcon sd_setImageWithURL:[NSURL URLWithString:userInfo.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
    }
}

- (void)buttonClick:(UIButton *)button{
    if (button.tag == 0 && self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoLikedList)]) {
        [self.delegate showUserInfoLikedList];
    }
    if (button.tag == 1 && self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoDynamicList)]) {
        [self.delegate showUserInfoDynamicList];
    }
    if (button.tag == 2 && self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoOpusList)]) {
        [self.delegate showUserInfoOpusList];
    }
    if (button.tag == 3 && self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoFansList)]) {
        [self.delegate showUserInfoFansList];
    }
}

@end
